package com.company.arithmetic;

import java.util.Scanner;

/**
 * Created by v on 16-12-15.
 */
public class BestFit {

    static int n; //物品总量
    static int m; //箱子大小
    static int[] goods;
    static TreeNode root;

    static int size=0;

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        n=input.nextInt();
        m=input.nextInt();
        goods=new int[n];

        for(int i=0;i<n;i++){
            goods[i]=input.nextInt();
        }

        for(int i=0;i<n;i++){
            encasement(goods[i]);
        }

        System.out.println(size);
    }

    //装箱算法
    private static void encasement(int element){
        TreeNode min=null;
        TreeNode current=root;
        TreeNode minParent=null;
        TreeNode parent=current;

        while (current!=null){
            if(element>current.element){
                parent=current;
                current=current.right;
            }else{
                minParent=parent;
                min=current;
                parent=current;
                current=current.left;
            }
        }

        if(min==null){
            insert(m-element);
            size++;
        }else {
            if (min.amount>1){
                min.amount--;
            }else if(min==root){
                TreeNode temp=new TreeNode();
                temp.left=root;
                remove(root,temp);
                root=temp.left;
            }else {
                remove(min,minParent);
            }

            if(min.element-element>0){
                insert(min.element-element);
            }
        }
    }

    //插入算法
    private static void insert(int element){
        TreeNode current=root;
        TreeNode parent=current;

        while (current!=null){
            parent=current;
            if(element<current.element){
                current=current.left;
            }else if(element>current.element) {
                current=current.right;
            }else {
                current.amount++;
                return;
            }
        }

        if(null ==parent) {
            root = new TreeNode(element);
        }else {
            if(element<parent.element){
                parent.left=new TreeNode(element);
            }else {
                parent.right=new TreeNode(element);
            }
        }
    }

    //删除算法
    private static void remove(TreeNode min,TreeNode parent){
        if(min.left==null){
            if(parent.left==min){
                parent.left=min.right;
            }else {
                parent.right=min.right;
            }
            return;
        }

        parent=min;
        TreeNode maxChild=min.left;
        while (maxChild.right!=null){
            parent=maxChild;
            maxChild=maxChild.right;
        }

        min.amount=maxChild.amount;
        min.element=maxChild.element;
        if(parent.left==maxChild){
            parent.left=maxChild.left;
        }else {
            parent.right=maxChild.left;
        }
    }

    private static class TreeNode{
        int element;
        TreeNode left;
        TreeNode right;
        int amount=1;

        TreeNode(){

        }

        TreeNode(int element){
            this.element=element;
        }
    }
}
